/*
 * hal_fileio.c
 *
 *  Created on: 2025 Mar 27
 *      Author: SFLY
 */
#include "hal_fileio.h"
/**file system config*************************************************************/
xt_hal_fileio_t g_stc_LogFile;

void Hal_Read_Motor_Param(xt_hal_fileio_t *pstc)
{
    pstc->isUserDefFlag = Flash_readWord(MOTOR_IS_CALIBRATION_ADDR);
    if(pstc->isUserDefFlag==1)//用户已经初始化过电机参数,直接从flash里读取电机参数
       {
            //读取电机参数
            pstc->motor_can_id  =  Flash_readWord(MOTOR_ID_ADDR);
            pstc->signflag = Flash_readWord(MOTOR_SIGNFLAG_ADDR);
            pstc->motor_elec_offset =  Flash_readWord(MOTOR_ELEC_OFFSET_ADDR);
            pstc->motor_mech_offset = Flash_readWord(MOTOR_MECH_OFFSET_ADDR);
            pstc->motor_orderdir = Flash_readWord(MOTOR_ORDERDIR_ADDR);
//            pstc->motor_pos_torque_kp = POS_TOR_DEFAULT_KP;
//            pstc->motor_pos_torque_kd = POS_TOR_DEFAULT_KD;
//            pstc->motor_speed_kp = SPD_DEFAULT_KP;
//            pstc->motor_speed_ki = SPD_DEFAULT_KI;
//            pstc->motor_pos_kp = POS_DEFAULT_KP;
//            pstc->motor_pos_ki = POS_DEFAULT_KI;
//            pstc->motor_pos_kd = POS_DEFAULT_KD;
//            pstc->motor_cur_kp = CUR_DEFAULT_KP;
//            pstc->motor_cur_ki = CUR_DEFAULT_KI;
//            pstc->motor_limit_cur_max = CUR_OUT_MAX;
//            pstc->motor_limit_spd_max = SPD_OUT_MAX;
            pstc->motor_pos_torque_kp = Flash_readWord(MOTOR_POS_TORQUR_KP_ADDR);
            pstc->motor_pos_torque_kd = Flash_readWord(MOTOR_POS_TORQUR_KD_ADDR);
            pstc->motor_speed_kp = Flash_readWord(MOTOR_SPEED_KP_ADDR);
            pstc->motor_speed_ki = Flash_readWord(MOTOR_SPEED_KI_ADDR);
            pstc->motor_pos_kp = Flash_readWord(MOTOR_POS_KP_ADDR);
            pstc->motor_pos_ki = Flash_readWord(MOTOR_POS_KI_ADDR);
            pstc->motor_pos_kd = Flash_readWord(MOTOR_POS_KD_ADDR);
            pstc->motor_cur_kp = Flash_readWord(MOTOR_CUR_KP_ADDR);
            pstc->motor_cur_ki = Flash_readWord(MOTOR_CUR_KI_ADDR);
            pstc->motor_limit_cur_max = Flash_readWord(MOTOR_LIMIT_CUR_MAX_ADDR);
            pstc->motor_limit_spd_max = Flash_readWord(MOTOR_LIMIT_SPD_MAX_ADDR);
            //读取误差表
            for(int i=0;i<LUT_N;i++)
            {
                pstc->motor_offset_lut[i]=Flash_readWord(LUT_ADDR+i*4);
            }
            #if ENCODER_STRUCT>0
                pstc->reducer_mech_offset = Flash_readWord(REDUCER_MECH_OFFSET_ADDR);
                pstc->reducer_orderdir = Flash_readWord(REDUCER_ORDERDIR_ADDR);
            #endif
       }
    else {
        //设定为默认值
        pstc->isUserDefFlag = 0;
        pstc->motor_can_id  =  MOTOR_CAN_ID;
        pstc->master_can_id =  MASTER_CANID_ADDR;
        pstc->motor_elec_offset = 0;
        pstc->motor_mech_offset = 0;
        pstc->motor_pos_torque_kp = POS_TOR_DEFAULT_KP;
        pstc->motor_pos_torque_kd = POS_TOR_DEFAULT_KD;
        pstc->motor_speed_kp = SPD_DEFAULT_KP;
        pstc->motor_speed_ki = SPD_DEFAULT_KI;
        pstc->motor_pos_kp = POS_DEFAULT_KP;
        pstc->motor_pos_ki = POS_DEFAULT_KI;
        pstc->motor_pos_kd = POS_DEFAULT_KD;
        pstc->motor_cur_kp = CUR_DEFAULT_KP;
        pstc->motor_cur_ki = CUR_DEFAULT_KI;
        pstc->motor_limit_cur_max = CUR_OUT_MAX;
        pstc->motor_limit_spd_max = SPD_OUT_MAX;
        pstc->motor_orderdir = 1;
        pstc->signflag = 0;
        for(int i=0;i<LUT_N;i++)
        {
            pstc->motor_offset_lut[i]=0;
        }
        #if ENCODER_STRUCT>0
            pstc->reducer_mech_offset = 0;
            pstc->reducer_orderdir = -1;
        #endif
    }
}

void Hal_Write_Motor_Param(xt_hal_fileio_t *pstc)
{
    Flash_eraseSector(SECTOR_NUM);
    Flash_eraseSector(LUT_SECTOR_NUM);
    Flash_writeWord(MOTOR_ID_ADDR,pstc->motor_can_id);
    Flash_writeWord(MOTOR_ELEC_OFFSET_ADDR,(uint32_t)(pstc->motor_elec_offset));
    Flash_writeWord(MOTOR_POS_TORQUR_KP_ADDR,(uint32_t)(pstc->motor_pos_torque_kp));
    Flash_writeWord(MOTOR_POS_TORQUR_KD_ADDR,(uint32_t)(pstc->motor_pos_torque_kd));
    Flash_writeWord(MOTOR_SPEED_KP_ADDR,(uint32_t)(pstc->motor_speed_kp));
    Flash_writeWord(MOTOR_SPEED_KI_ADDR,(uint32_t)(pstc->motor_speed_ki));
    Flash_writeWord(MOTOR_POS_KP_ADDR,(uint32_t)(pstc->motor_pos_kp));
    Flash_writeWord(MOTOR_POS_KI_ADDR,(uint32_t)(pstc->motor_pos_ki));
    Flash_writeWord(MOTOR_POS_KD_ADDR,(uint32_t)(pstc->motor_pos_kd));
    Flash_writeWord(MOTOR_CUR_KP_ADDR,(uint32_t)(pstc->motor_cur_kp));
    Flash_writeWord(MOTOR_CUR_KI_ADDR,(uint32_t)(pstc->motor_cur_ki));
    Flash_writeWord(MOTOR_MECH_OFFSET_ADDR,(uint32_t)(pstc->motor_mech_offset));
    Flash_writeWord(REDUCER_MECH_OFFSET_ADDR, (uint32_t)(pstc->reducer_mech_offset));
    Flash_writeWord(MOTOR_LIMIT_CUR_MAX_ADDR,(uint32_t)(pstc->motor_limit_cur_max));
    Flash_writeWord(MOTOR_LIMIT_SPD_MAX_ADDR,pstc->motor_limit_spd_max);
    Flash_writeWord(MOTOR_IS_CALIBRATION_ADDR,(uint32_t)(pstc->isUserDefFlag));
    Flash_writeWord(MOTOR_SIGNFLAG_ADDR,(uint32_t)(pstc->signflag));
    Flash_writeWord(MOTOR_ORDERDIR_ADDR,(uint32_t)(pstc->motor_orderdir));
    Flash_writeWord(REDUCER_ORDERDIR_ADDR,(uint32_t)(pstc->reducer_orderdir));
    Flash_writeWord(MASTER_CANID_ADDR,(uint32_t)(pstc->master_can_id));
      //写入误差表
      for(int i=0;i<LUT_N;i++)
      {
          Flash_writeWord(LUT_ADDR+i*4,pstc->motor_offset_lut[i]);
      }
}



